﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Common;

namespace He.Web.manage.weixin
{
    public partial class config : He.Web.UI.Manage
    {
       public  Model.wx_config wx_config = new BLL.wx_config().LoadConfig();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                chkLoginLevel("editWebset");
                ShowInfo();
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            txtWxName.Text = wx_config.wx_name;
            txtWxId.Text = wx_config.wx_id;
            if (!string.IsNullOrEmpty(wx_config.figure_url))
            {
                litFigureUrl.Text = string.Format("<img src=\"{0}\" width=\"110\" />", wx_config.figure_url);
            }
            txtAppid.Text = wx_config.appid;
            txtAppsecret.Text = wx_config.appsecret;
            txtToken.Text = wx_config.token;
            txtFollowMsg.Text = wx_config.follow_msg;
            cbFollowIsKeyword.Checked = wx_config.follow_is_keyword == 1;
            txtFollowKeyword.Text = wx_config.follow_keyword;
            txtUnAnswerMsg.Text = wx_config.un_answer_msg;
            cbUnAnswerIsKeyword.Checked = wx_config.un_answer_is_keyword == 1;
            txtUnAnswerKeyword.Text = wx_config.un_answer_keyword;
        }
        #endregion
        
        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            wx_config.wx_name = txtWxName.Text.Trim();
            wx_config.wx_id = txtWxId.Text.Trim();
            HttpPostedFile _posteFigure = upFigureUrl.PostedFile;
            if (_posteFigure != null && _posteFigure.FileName != string.Empty)
            {
                string _fileExt = _posteFigure.FileName.Substring(_posteFigure.FileName.LastIndexOf(".") + 1);
                //验证合法的文件
                if (!FileHelper.CheckFileExt(UploadImgExt, _fileExt))
                {
                    ShowError("不允许上传" + _fileExt + "类型的文件,请重试。");
                    return;
                }
                if (_posteFigure.ContentLength > webset.upload_file_size * 1024)
                {
                    ShowError("图片超过限制，请先压缩图片。");
                    return;
                }
                FileHelper.DeleteFile(wx_config.figure_url);
                string _figureurl = "/images/sys/wxfigure." + _fileExt;
                _posteFigure.SaveAs(Server.MapPath(_figureurl));
                wx_config.figure_url = _figureurl;
            }
            wx_config.appid = txtAppid.Text.Trim();
            wx_config.appsecret = txtAppsecret.Text.Trim();
            wx_config.token = txtToken.Text.Trim();
            wx_config.follow_msg = txtFollowMsg.Text;
            wx_config.follow_is_keyword = cbFollowIsKeyword.Checked ? 1 : 0;
            wx_config.follow_keyword = txtFollowKeyword.Text.Trim();
            wx_config.un_answer_msg = txtUnAnswerMsg.Text;
            wx_config.un_answer_is_keyword = cbUnAnswerIsKeyword.Checked ? 1 : 0;
            wx_config.un_answer_keyword = txtUnAnswerKeyword.Text.Trim();
            new BLL.config().SaveConfig<Model.wx_config>(wx_config, SysKeys.WEIXIN_CONFIG_PATH);
            SaveLogs("[微信公众号管理]参数设置");
            ShowSuccess("信息修改成功啦！", Request.RawUrl);
        }

        //删除头像
        protected void lbtnDelFigureUrl_Click(object sender, EventArgs e)
        {
            FileHelper.DeleteFile(wx_config.figure_url);
            wx_config.figure_url = "";
            new BLL.config().SaveConfig<Model.wx_config>(wx_config, SysKeys.WEIXIN_CONFIG_PATH);
            Response.Redirect(Request.RawUrl);
        }
    }
}